package com.miwzn.boot.modules.sys.web;

import com.miwzn.base.AbstractController;
import com.miwzn.boot.modules.sys.entity.dto.SysRolePermissionRelDto;
import com.miwzn.boot.modules.sys.entity.po.SysRolePermissionRel;
import com.miwzn.boot.modules.sys.entity.qm.SysRolePermissionRelQuery;
import com.miwzn.boot.modules.sys.service.SysRolePermissionRelService;
import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.annotation.OperationLog;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wzn
 * @Date: 2018-12-27 14:46:31
 * @Description: www.miwzn.com mvc api
 */
@ResponseBody
@Controller
@RequestMapping("sys/role/permission/rel")
public class SysRolePermissionRelController extends AbstractController<SysRolePermissionRel, SysRolePermissionRelDto, SysRolePermissionRelQuery> {
    private static Logger logger = LoggerFactory.getLogger(SysRolePermissionRelController.class);

    @Autowired
    private SysRolePermissionRelService sysRolePermissionRelService;

    /**
     * 列表
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-角色-权限",content ="获取角色-权限列表")
    @RequiresPermissions("sys:role:permission:rel:list")
    @GetMapping("/list")
    public ResponseEntity list(SysRolePermissionRelQuery entity) {
        return super.listD(entity);
    }

    /**
     * 添加
     *
     * @param entity
     *
     * @return
     */
    @OperationLog(operation = "系统-角色-权限",content ="分配角色-权限")
    @RequiresPermissions("sys:role:permission:rel:adds")
    @PostMapping("adds")
    public ResponseEntity adds(@RequestBody List<SysRolePermissionRelDto> entity) {
        del(entity);
        return del(entity) ? super.adds(entity) : ResponseEntity.success("保存成功");
    }

    private Boolean del(List<SysRolePermissionRelDto> entity) {
        if (CollectionUtils.isNotEmpty(entity)) {
            SysRolePermissionRel sysRolePermissionRel = new SysRolePermissionRel();
            sysRolePermissionRel.setRoleId(entity.get(0).getRoleId());
            sysRolePermissionRelService.delete(sysRolePermissionRel);
        }
        if (CollectionUtils.isEmpty(entity) || (CollectionUtils.isNotEmpty(entity) && entity.size() == 1 && entity.get(0).getPermissionId() == null)) {
            return false;
        } else {
            return true;
        }
    }
}
